﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using ApplicationBll;

namespace wallet.WebHandler
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class stockHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            switch (context.Request["action"].ToLower())
            {
                case "stockinfo":
                    StockInfo(context);
                    break;
                case "addmystockinfo":
                    AddMyStockInfo(context);
                    break;
                case "removemystockinfo":
                    RemoveMyStockInfo(context);
                    break;
                default:
                    break;
            }
        }
        private void RemoveMyStockInfo(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string code = context.Request.Form["code"];
            Stock stock = new Stock();
            int count = stock.RemoveMyStockInfo(code);
            context.Response.Write("移除" + count + "条记录");
        }
        private void AddMyStockInfo(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string code = context.Request.Form["code"];
            Stock stock = new Stock();
            if (stock.ValidateStockCode(code))
            {
                stock.AddMyStockInfo(code);
                context.Response.Write("添加成功");
            }
            else
            {
                context.Response.Write("股票代码错误");
            }
        }

        private void StockInfo(HttpContext context)
        {
            context.Response.ContentType = "application/jsonrequest";
            int start = int.Parse(context.Request.Form["start"]);
            int limit = int.Parse(context.Request.Form["limit"]);
            Stock stock = new Stock();
            string json = stock.GetJSONStockInfo(context.Request.Form["codePre"], start, limit);
            context.Response.Write(json);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
